package main

import (
	"context"
	"fmt"
	"go.etcd.io/etcd/clientv3"
	"time"
)

func main() {
	client, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"127.0.0.1:2379"},
		RetryDialer: nil,
		DialTimeout: time.Second * 5,
		TLS:         nil,
	})
	if err != nil {
		fmt.Printf("connect to etcd failed,err :%v", err)
		return
	}
	fmt.Println("连接成功", client)
	defer client.Close()
	//put
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	str := `[{"path":"D:\\老男孩\\logsAgent\\s4.log","topic":"web_log"}]`
	_, err = client.Put(ctx, "collect_log_key", str)
	if err != nil {
		fmt.Printf("put to etcd failed,err:%v", err)
		return
	}
	cancel()

	//get
	ctx, cancel = context.WithTimeout(context.Background(), time.Second)
	gr, err := client.Get(ctx, "collect_log_key")
	if err != nil {
		fmt.Printf("get from etcd failed,err:%v", err)
		return
	}
	for _, ev := range gr.Kvs {
		fmt.Printf("key:%s,values:%s\n", ev.Key, ev.Value)
	}
	cancel()

}
